﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;

public partial class QuarterPlan_QuarterPlanMeet : System.Web.UI.Page
{
    IBussinessLogic dllUserInfo = new UserInfoManager();
    WeekDateManager dllWeekDateManager = new WeekDateManager();
    WeekMeetRecord dllWeekMeetRecord = new WeekMeetRecord();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    Authentication dllAuthentication = new Authentication();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    QuarterPlanDetailManager dllQuarterPlanDetail = new QuarterPlanDetailManager();
    IBussinessLogic dllQuarterPlanDetailManager = new QuarterPlanDetailManager();
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        this.btnAdd.Enabled = false;
        if (!IsPostBack)
        {
            bindDropDownList(this.drpDepartment);
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpQuarterDate); 
            BindGridView();
        }

    }

    public string getstring(string str, int num)
    {

        return (str.Length > num) ? str.Substring(0, num) : str;
    }

    /// <summary>
    /// 绑定GridView
    /// </summary>
    void BindGridView()
    {
        this.gdvMeetRecord.Columns[5].Visible = false;
        this.gdvMeetRecord.Columns[6].Visible = false;
        object[] _value1 = new object[1];
        _value1[0] = objUserInfo.UserName;
         
        DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _value1);
        if (_userTable.Rows.Count > 0)
        {
            if (_userTable.Rows[0]["DepartmentId"].ToString().Length > 0)
            {
                if (this.drpDepartment.SelectedItem.Text == _userTable.Rows[0]["DepartmentId"].ToString())
                {
                    this.btnAdd.Enabled = true;
                    this.gdvMeetRecord.Columns[5].Visible = true;
                    this.gdvMeetRecord.Columns[6].Visible = true;
                }
                else
                {
                    this.btnAdd.Enabled = false;
                    this.gdvMeetRecord.Columns[5].Visible = false;
                    this.gdvMeetRecord.Columns[6].Visible = false;
                }
            }
            if (objUserInfo.UserName == "admin")
            {
                this.btnAdd.Enabled = true;
                this.gdvMeetRecord.Columns[5].Visible = true;
                this.gdvMeetRecord.Columns[6].Visible = true;
            }

        }
        if (this.drpWeekDate.SelectedItem.Text != "无周周期")
        {
            object[] _value = new object[2];
            _value[0] = Convert.ToInt32(this.drpWeekDate.SelectedItem.Value);
            _value[1] = this.drpDepartment.SelectedItem.Text;
            this.gdvMeetRecord.DataSource = dllWeekMeetRecord.get(WeekMeetRecord.field.WeekDate.ToString(), _value);
            this.gdvMeetRecord.DataBind();
            for (int i = 0; i < this.gdvMeetRecord.Rows.Count; i++)
            {
                if (this.gdvMeetRecord.DataKeys[i]["MeetPath"].ToString().Length == 0)
                {
                    Button _btn = (Button)this.gdvMeetRecord.Rows[i].Cells[3].FindControl("btnDownLoad");
                    _btn.Visible = false;
                    Label _lbl = (Label)this.gdvMeetRecord.Rows[i].Cells[3].FindControl("lblAlert");
                    _lbl.Visible = true;
                    _lbl.Text = "无下载";
                }
                #region

                this.gdvMeetRecord.Rows[i].Cells[3].Text = Utility.Comm.NoHTML(this.gdvMeetRecord.Rows[i].Cells[3].Text);

                #endregion
            }
        }
        else
        {
            this.gdvMeetRecord.DataSource = new DataTable();
            this.gdvMeetRecord.DataBind();
        }


    }
    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            DataTable _year = dllQuarterDateManager.getQuarterDate();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        }
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                }
                #endregion
                bindDropDownList(this.drpMonthDate);
            }
            else
            {
                ListItem li = new ListItem("无年周期");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(li);
                bindDropDownList(this.drpMonthDate);
            }
        }
        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                DataTable _monthtime = dllMonthDateManager.getMonthDate();
                if (_monthtime.Rows.Count == 1)
                {
                    this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                    this.drpMonthDate.DataSource = _monthtime;
                    this.drpMonthDate.DataTextField = "MonthName";
                    this.drpMonthDate.DataValueField = "Month_Id";

                }
                #endregion
                bindDropDownList(this.drpWeekDate);
            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);
                this.gdvMeetRecord.DataSource = new DataTable();
                this.gdvMeetRecord.DataBind();
                bindDropDownList(this.drpWeekDate);
            }

        }
        //绑定周计划周期
        if (_drp.Equals(this.drpWeekDate))
        {
            object[] _value = new object[3];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            _value[2] = this.drpMonthDate.SelectedItem.Text;
            DataTable _week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
            if (_week.Rows.Count != 0)
            {
                this.drpWeekDate.DataSource = _week;
                this.drpWeekDate.DataTextField = "WeekName";
                this.drpWeekDate.DataValueField = "Week_Id";
                this.drpWeekDate.DataBind();
                DataTable _weektime = dllWeekDateManager.getWeekDate();
                if (_weektime.Rows.Count > 0)
                {
                    this.drpWeekDate.SelectedIndex = this.drpWeekDate.Items.IndexOf(this.drpWeekDate.Items.FindByText(_weektime.Rows[0][4].ToString()));


                }
                BindGridView();
            }
            else
            {
                ListItem _li = new ListItem("无周周期");
                this.drpWeekDate.Items.Clear();
                this.drpWeekDate.Items.Add(_li);
                this.gdvMeetRecord.DataSource = new DataTable();
                BindGridView();
            }
        }
        if (_drp.Equals(this.drpDepartment))
        {
            this.drpDepartment.DataSource = dllDepartment.getNoTree();
            this.drpDepartment.DataTextField = "DepartmentName";
            this.drpDepartment.DataValueField = "DepartmentId";
            this.drpDepartment.DataBind();
            ListItem _li = new ListItem("请选择部门");
            this.drpDepartment.Items.Insert(0, new ListItem("公司会议"));
            object[] _value = new object[1];
            _value[0] = objUserInfo.UserName;
            DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _value);
            if (_userTable.Rows.Count > 0)
            {
                this.drpDepartment.SelectedIndex = this.drpDepartment.Items.IndexOf(this.drpDepartment.Items.FindByText(_userTable.Rows[0]["DepartmentId"].ToString()));
            }
        }
    }
    /// <summary>
    /// 添加按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Utility.HttpHelp.redirectPage("WeekPlanMeetAdd.aspx?Year=" + this.drpYear.SelectedValue + "&QuarterDate=" + this.drpQuarterDate.SelectedValue + "&MonthDate=" + this.drpMonthDate.SelectedValue + "&WeekDate=" + this.drpWeekDate.SelectedValue+"&department=" + this.drpDepartment.SelectedItem.Text);
    }
    /// <summary>
    /// 年份索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        bindDropDownList(this.drpMonthDate);
        if (this.drpMonthDate.Text != "无月周期")
        {
            BindGridView();
        }
        else
        {
            this.gdvMeetRecord.DataSource = new DataTable();
            this.gdvMeetRecord.DataBind();
        }

    }
    /// <summary>
    /// 年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);
    }
    /// <summary>
    /// 下载按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvMeetRecord.DataKeys[i]["MeetPath"].ToString();


        string filepath = Server.MapPath("~/");
        FileDownload(filepath + "\\Upload\\" + path);
        //Response.Redirect("~/upload/" + path);
        //   Utility.HttpHelp.DownloadFile(path);
    }

    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }
    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {

        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.gdvMeetRecord.DataKeys[i]["MeetId"].ToString());

        int result = dllWeekMeetRecord.del(WeekMeetRecord.field.MeetId.ToString(), _value);
        if (result < 0)
        {
            Utility.HttpHelp.alert("删除会议管理信息未成功，请重试！", Page);

        }
        else
        {
            Response.Redirect("WeekPlanMeet.aspx");
        }
    }
    /// <summary>
    /// 月周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpWeekDate);
    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvMeetRecord_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gdvMeetRecord.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void drpWeekDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView();
    }
    protected void gdvMeetRecord_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sid = this.gdvMeetRecord.DataKeys[e.RowIndex]["MeetId"].ToString();
        Response.Redirect("WeekPlanMeetAdd.aspx?id=" + sid + "&department=" + this.drpDepartment.SelectedItem.Text);
    }
    protected void gdvMeetRecord_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.gdvMeetRecord.DataKeys[e.RowIndex]["MeetId"].ToString());

        int result = dllWeekMeetRecord.del(WeekMeetRecord.field.MeetId.ToString(), _value);
        if (result < 0)
        {
            Utility.HttpHelp.alert("删除会议管理信息未成功，请重试！", Page);

        }
        else
        {
            Response.Redirect("WeekPlanMeet.aspx");
        }
    }

    /// <summary>
    /// 自动编号
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvMeetRecord_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }
    }
    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView();
    }
}
